﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EnvUIShow : MonoBehaviour
{
        [Header("黑板上的文字")]
        public TextMesh[] txtBlackBoard;
        [Header("实验桌上的文字")]
        public TextMesh[] txtLabDesktop;
        [Header("网络指示器")]
        public Text netText;

        public bool isConnection { get; set; }

        public int showTime = 2000;

        public bool isReciveData = false;

        public int h1Max = 9;
        public int h2Max = 20;

        public int c1Max = 10;
        public int c2Max = 10;

        private TextMesh updateText;
        private string updateContext;
        private bool isUpdate;

        private bool isH1Update;
        private string updateH1Context;

        private long timeStart;
        private bool isShow;

        private float timeSumH1;
        private float timeSumH2;

        private float irdTime;
        private bool isNetShowG = false;
        void Update()
        {
                {//网络指示
                        if (isReciveData)
                        {
                                irdTime = 0;
                                isReciveData = false;
                                isNetShowG = true;
                        }

                        if (isNetShowG)
                        {
                                irdTime += Time.deltaTime;
                                if (irdTime > 0.05)
                                {
                                        isNetShowG = false;
                                }
                        }
                }

                if (isUpdate)
                {
                        updateText.text = updateContext;
                }

                {//H1的处理
                        if (isH1Update)
                        {
                                isH1Update = false;
                                isShow = true;
                                timeStart = Timestamp.GetTimeStamp();
                                if (txtBlackBoard.Length > 0)
                                {
                                        if (updateH1Context != null)
                                        {
                                                txtBlackBoard[0].text = updateH1Context;
                                        }
                                }
                        }

                        if (isShow)
                        {
                                if (Timestamp.GetTimeStamp() - timeStart > showTime)
                                {
                                        isShow = false;
                                }
                        }
                        else
                        {
                                timeSumH1 += Time.deltaTime;
                                if (timeSumH1 > 3f)
                                {
                                        timeSumH1 = 0;
                                        if (txtBlackBoard.Length > 0)
                                        {
                                                txtBlackBoard[0].text = GetRandomPost();
                                        }
                                }
                        }
                }

                //H2
                if (txtBlackBoard.Length > 1)
                {
                        timeSumH2 += Time.deltaTime;
                        if (timeSumH2 > 1)
                        {
                                timeSumH2 = 0;
                                txtBlackBoard[1].text = "百全楼316实验室\n" + DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒");
                        }
                }

                //网络通讯指示
                if (isNetShowG)
                {
                        netText.color = Color.red;
                }
                else
                {
                        netText.color = Color.white;
                }

                if (isConnection)
                {
                        netText.text = "@";
                }
                else
                {
                        netText.text = "~";
                }
        }

        public void H1(string str)
        {
                if (txtBlackBoard != null)
                {
                                if (str.Length > h1Max)
                                {
                                        str = str.Substring(0, h1Max);
                                }

                                updateH1Context = str;
                                isH1Update = true;
                }
        }

        public void H2(string str)
        {
                if (txtBlackBoard != null)
                {
                        if (txtBlackBoard.Length > 1)
                        {
                                if (str.Length > h2Max)
                                {
                                        str = str.Substring(0, h2Max);
                                }

                                updateText = txtBlackBoard[1];
                                updateContext = str;
                                isUpdate = true;
                        }
                }
        }

        public void C1(string str)
        {
                if (txtLabDesktop != null)
                {
                        if (txtLabDesktop.Length > 0)
                        {
                                if (str.Length > c1Max)
                                {
                                        str = str.Substring(0, c1Max);
                                }

                                updateText = txtLabDesktop[0];
                                updateContext = str;
                                isUpdate = true;
                        }
                }
        }

        public void C2(string str)
        {
                if (txtLabDesktop != null)
                {
                        if (txtLabDesktop.Length > 1)
                        {
                                if (str.Length > c2Max)
                                {
                                        str = str.Substring(0, c2Max);
                                }

                                updateText = txtLabDesktop[1];
                                updateContext = str;
                                isUpdate = true;
                        }
                }
        }
        private string GetRandomPost()
        {
                if (post.Length > 0)
                {
                        int random = UnityEngine.Random.Range(0, post.Length);
                        return post[random];
                }
                return null;
        }


        string[] post = { 
                        "陌上人如玉，公子世无双", 
                        "远芳侵古道，晴翠接荒城",
                        "电压的分配与电阻成正比。",
                        "一元弱酸、弱碱一步电离。",
                        "Time tries all things.",
                        "Mind act upon mind.",
                        "若走错方向，停止就是进步。",
                        "有些故事，请不必追问结局。",
                        "同一个世界，同一个梦想",
                        "不为遇见，只为远方",
                        "不畏将来，不念过去",
                        "愿你与这世界温暖相拥。",
                        "时光走过，留下岁月的年轮。",
                        "细雨万点，入千般画意。",
                        "想想自己最初得到时的快乐。",
                        "天高，云淡，一路有你相伴。",
                        "信任，即诗，即远方。",
                        "念与不念，朋友永远相伴。",
                        "一夏的努力，一秋的稻花香。",
                        "空谈误国，实干兴邦。",
                        "不忘初心，牢记使命",
                        "穷且益坚，不坠青云之志。"};

}
